local cms_model = require("models.dao.cms")
local IndexController = {}

--首页
function IndexController:index()
    local req = self:getRequest()
    local page = req:getParam('page') or 1
    local rows = req:getParam('rows') or 10
    local status = req:getParam('status') or 1
    local cateId =  req:getParam('cate') or 0
    local posts_result = cms_model:posts_list(page,rows,status,cateId)
    local view = self:getView()
    local p = {
        list = posts_result
    }
    local template_name = "index/index.html"
    return view:render(template_name, p)
end

--查看文章
function IndexController:detail()
    local pid = self.params['id']
    --这个是restful里面定义pattern的参数 ,pattern = '/:catepath/:id'
    local site_path = self.params['catepath']
    pid = tonumber(pid)
    --判断参数是否是数字，不是数值404
    if pid == nil or pid==0 then
        ngx.exit(404)
    end

    --得到文章内容

    local posts_infos_result = cms_model:posts_infos(pid)
    local view = self:getView()
    local p = {
        posts = posts_infos_result[1]
    }
    -- 使用自定义模板路径
    local template_name = "index/detail.html"
    -- return 必须返回一个字符串
    return view:render(template_name, p)
end

return IndexController
